<template>
	<view class="content openWz-container">
		<view class="title">手机号码验证</view>
		<view class="tip f-24">使用系统前需开通居民身份网络可信凭证（CTID）</view>
		
		<view class="input-box">
			<view class="uni-form-item uni-column">
				<view class="label flexbox flexbox-middle">手机：</view>
				<input class="uni-input" disabled v-model="formData.tel" placeholder="请输入手机号码" />
			</view>
			
			<view class="uni-form-item uni-column">
				<view class="label flexbox flexbox-middle">验证码：</view>
				<view class="input-view">
					<input type="number" class="uni-input" @input="onKeyInput" maxlength="6" v-model="formData.vcode" placeholder="请输入验证码" />
					<view @click="getCode" class="code-btn f-22 flexbox flexbox-middle flexbox-center" :class="{'disabled-style': !sendFlag}">{{sendTxt}}</view>
				</view>
			</view>
		</view>
		
		<view class="open-btn flexbox flexbox-middle flexbox-center f-32" @click="handleSubmit" :class="{'disabled':!activeFlag}">提交</view>
		
	</view>

</template>

<script>
	import { pushData } from '@/static/js/utils.js'
	export default {
		data() {
			return {
				formData: {
					tel: uni.getStorageSync('phone'),
					vcode: ''
				},
				activeFlag: false,
				sendFlag: true,
				sendTxt: '获取验证码',
				id: '',
				btnFlag: true,
				type: '',
				commonId: '',
				scan_flag:'',
				rurl: '',
				query: {}
			}
		},
		onLoad: function(e) {
			this.query = e
			this.id = e.id
			this.type = e.type
			this.commonId = e.commonId
			this.scan_flag = e.scan_flag || ''
			this.rurl = e.rurl || ''
		},
		methods: {
			onKeyInput() {
				if(this.formData.vcode.length == 6) {
					this.activeFlag = true
				} else {
					this.activeFlag = false
				}
			},

			getCode() {
				if (this.sendFlag) {
				    this.sendFlag = false
					this.$HTTP.post('/ctid/unity', {type: this.$BSNCODE.V_CODE}, false).then((r) => {
						let param = {
							phone: this.formData.tel,
							bsn2: uni.getStorageSync('firstBsn'),
							bsn: r.data.bsn
						}
						this.$HTTP.post('/audit/vcode', param, true).then((res) => {
						    let num = 60;
						    let autoDown = setInterval(() => {
						        num--;
						        let str = num + '秒后获取';
						        if (num < 0) {
						            this.sendTxt = '获取验证码'
						            this.sendFlag = true
						            clearInterval(autoDown);
						        } else {
						            this.sendTxt = str
						        }
						    }, 1000)
						}).catch((error) => {
						    this.sendFlag = true
						});
					}).catch(err => {
						
					})
					
				} else {
					uni.showToast({
					    title: '请稍后，发送太频繁！',
					    icon: 'none'
					})
				}
				
			},
			bindCompany(company_id) {
				let param = {
					company_id: company_id
				}
				this.$HTTP.post('/user/binded', param, true).then((res) => {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				}).catch((err) => {
					
				})
			},
			handleSubmit() {
				if (this.formData.vcode.length != 6) {
					uni.showToast({
					    title: '验证码为6位数',
					    icon: 'none'
					})
					return false
				}
				if (this.formData.vcode.length == 6) {
					if (this.btnFlag) {
						this.btnFlag = false
						this.$HTTP.post('/ctid/unity', {type: this.$BSNCODE.V_CODE_AUTH}, false).then((r) => {
							let param2 = {
								phone: this.formData.tel,
								bsn2: uni.getStorageSync('firstBsn'),
								vcode: this.formData.vcode,
								id: this.id,
								bsn: r.data.bsn
							}
							
							this.$HTTP.post('/audit/vcodeauth', param2, true).then((res) => {
								let ctid = res.data.ctid
								uni.setStorageSync('ctid', ctid);
								pushData()
								if(this.rurl) {
									uni.redirectTo({
									    url: `${this.rurl}&ctid=${ctid}`
									})
									return false
								} else if(this.type === 'applyCertificate') {
									uni.redirectTo({
									    url: `/pages/applyCertificate/index?template_id=${this.commonId}&ctid=${ctid}`
									})
								} else if(this.type === 'getCertificate'){
									uni.redirectTo({
									    url: `/pages/getCertificate/index?id=${this.query.id}&phone=${this.query.phone}&ctid=${ctid}`
									})
								} else if(this.type === 'wzcard'){
									uni.redirectTo({
									    url: `/pages/wzcard/index?ctid=${ctid}`
									})
								} else if(this.type === 'companyAuth'){
									uni.redirectTo({
									    url: `/pages/companyAuth/index?scan_flag=${this.scan_flag}&ctid=${ctid}`
									})
								} else if(this.type == 'authOperation') {
									this.bindCompany(this.commonId)
									uni.reLaunch({
									    url: `/pages/authStatus/index?type=1&ctid=${ctid}`
									});
								} else {
									uni.reLaunch({
									    url: `/pages/authStatus/index?type=1&ctid=${ctid}`
									});
								}
								
							}).catch((error) => {
							    uni.navigateTo({
							        url: `/pages/authStatus/index?type=2&msg=${err.msg}&id=${this.query.commonId}&authtype=${this.query.type}&scan_flag=${this.query.scan_flag}&rurl=${this.query.rurl}&phone=${this.query.phone}`
							    });
							}).finally(() => {
								this.btnFlag = true
							});
						})
					}
					
				}	
			}
		},
	}
</script>

<style lang="scss" scoped>
	@import "../openWz-step1/index.scss";
</style>